package com.sfzd5.exam.helplibrary.exam.answersheet;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
/*
记录题目，每题几个小题，每个小题多少空，每空多少空格数
每行7mm高

        每行180mm - 6mm， 通过计算得出换行点
*/
public class BlankAnswerSheetQuestion extends AnswerSheetQuestion {
    public BlankAnswerSheetQuestion(){
        areaType = Area.AreaType.Blank;
    }
    public BlankAnswerSheetQuestion(AnswerSheetQuestion answerSheetQuestion)
    {
        this.id = answerSheetQuestion.id;
        this.areaType = answerSheetQuestion.areaType;
        this.infoJson = answerSheetQuestion.infoJson;
        this.questionId = answerSheetQuestion.questionId;
        this.answer = answerSheetQuestion.answer;
        this.score = answerSheetQuestion.score;
    }

    public List<List<BlankInfoDraw>> getLines(){
        //行宽 内部宽度 210 - 15 * 2 - 2 * 2 = 176mm - 6mm = 170mm
        int allWidth = 170;
        List<List<BlankInfoDraw>> lines = new ArrayList<>();
        List<BlankInfoDraw> line = new ArrayList<>();
        int addWidth = 0;

        for(BlankInfo blankInfo : getBlankInfoList()) {
            // 当前行还剩余的宽度
            int canAdd_Count = 170 - addWidth;

            BlankInfoDraw blankInfoDraw = new BlankInfoDraw(blankInfo);
            //本行能放得下，就放在本行
            if(canAdd_Count >= blankInfo.width + 4) {
                line.add(blankInfoDraw);
                addWidth += blankInfo.width + 4;
            } else {
                //本行放不下，不可以将本空全部添加到本行，计算剩余的_字符数
                int sykg = blankInfo.width + 4;

                // 如果本行最少有最少有15个位置，并且转到下行的字数超过7个本空格字节数大于15，
                if (canAdd_Count >= 14) {
                    blankInfoDraw.drawWidth = canAdd_Count - 4;
                    line.add(blankInfoDraw);

                    //本空剩余的部分
                    sykg -= canAdd_Count;
                }

                //如果剩余_个数不为0则一直添加到完，支持超长空格
                while (sykg > 0) {
                    lines.add(line);
                    line = new ArrayList<>();

                    //将前面添加的内容添加到列表里
                    blankInfoDraw = new BlankInfoDraw(blankInfo);
                    blankInfoDraw.drawBox = false;
                    blankInfoDraw.drawWidth = Math.min(allWidth, sykg);
                    sykg -= blankInfoDraw.drawWidth;
                    line.add(blankInfoDraw);
                }
                addWidth = blankInfoDraw.drawWidth;
            }
        }
        lines.add(line);
        return lines;
    }

    public List<BlankInfo> getBlankInfoList() {
        if(blankInfoList==null){
            if(infoJson!=null && !infoJson.isEmpty()){
                Type type = new TypeToken<ArrayList<BlankInfo>>(){}.getType();
                blankInfoList = new Gson().fromJson(infoJson, type);
            } else {
                blankInfoList = new ArrayList<>();
            }
        }
        return blankInfoList;
    }

    public void setBlankInfoList(List<BlankInfo> blankInfoList) {
        this.blankInfoList = blankInfoList;
        this.infoJson = new Gson().toJson(blankInfoList);
    }

    private List<BlankInfo> blankInfoList = null;

    @Override
    public String toString() {
        return String.format("填空题");
    }
}
